﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Skyhook.Core.Graphics;
using Skyhook.Core.Helpers;
using Skyhook.Resources;

namespace Skyhook.Landscape
{
    struct LandVertex
    {
        public Vector3 Position;
        public Vector2 TexCoord;
        public Vector2 MixCoord;
    }

    class LandscapeModel
    {
        public int MapWidth { get; private set; }
        public int MapHeight { get; private set; }

        public Image[] LandTextures { get; private set; }
        public Image[] MixTextures { get; private set; }

        public VertexBuffer VertexBuffer { get; private set; }
        VertexDeclaration VertexDeclaration = null;

        public LandscapeModel()
        {
            MapWidth = 16;
            MapHeight = 16;

            LandTextures = null;
            MixTextures = null;

            VertexBuffer = null;
        }
        
        public void Initialize()
        {
            LandTextures = new Image[6];
            MixTextures = new Image[2];

            LandTextures[0] = ResourceFactory.CreateFromFile<Image>("cement");
            LandTextures[1] = ResourceFactory.CreateFromFile<Image>("grass");
            LandTextures[2] = ResourceFactory.CreateFromFile<Image>("water");
            LandTextures[3] = ResourceFactory.CreateFromFile<Image>("sand");
            LandTextures[4] = ResourceFactory.CreateFromFile<Image>("grass");
            LandTextures[5] = ResourceFactory.CreateFromFile<Image>("water");
            MixTextures[0] = ResourceFactory.CreateFromFile<Image>("mix");
            MixTextures[1] = ResourceFactory.CreateFromFile<Image>("mix2");

            SetupVertexBuffer();
        }

        void SetupVertexBuffer()
        {
            VertexDeclaration = new VertexDeclaration(new VertexElement[]
                {
                    new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
                    new VertexElement(12, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0),
                    new VertexElement(20, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 1)
                }
            );

            VertexBuffer = GraphicsFactory.CreateVertexBuffer(VertexDeclaration, (MapHeight + 1) * (MapWidth + 1), BufferUsage.WriteOnly);
            
            LandVertex[] LandData = new LandVertex[(int)(MapWidth + 1) * (int)(MapHeight + 1)];
            for (int i = 0; i < MapHeight + 1; i++)
            {
                for (int j = 0; j < MapWidth + 1; j++)
                {
                    LandData[i * (MapWidth + 1) + j].Position = new Vector3(j, i, 0);
                    LandData[i * (MapWidth + 1) + j].TexCoord = new Vector2(1.0f * i, 1.0f * j);
                    LandData[i * (MapWidth + 1) + j].MixCoord = new Vector2(1.0f / MapWidth * i, 1.0f / MapHeight * j);
                }
            }
            VertexBuffer.SetData<LandVertex>(LandData);
        }
    }
}
